home *** CD-ROM | disk | FTP | other *** search
Lisp/Scheme | 1988-04-07 | 8.6 KB | 272 lines | [TEXT/ttxt] |
- ;; Larry Mulcahy 1988
- ;; String functions and constants
-
- (provide 'string)
- (require 'apl)
- (require 'math)
- (require 'sequence)
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; string-search
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; return the 0-origin position of the first occurrence of the
- ; substring sub in the string s.
- ; If not found, return nil.
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun string-search (sub s) (string-search-helper sub s 0))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; string-search-helper
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun string-search-helper (sub s deep)
- (let
- ((l-sub (length sub))
- (l-s (length s)))
- (if (> l-sub l-s)
- nil
- (if (equal (subseq s 0 l-sub) sub)
- deep
- (string-search-helper sub (string-rest s) (1+ deep))))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; string-substitute
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun string-substitute (old new s)
- (if (= (length s) 0)
- s
- (let
- ((where (string-search old s)))
- (if where
- (strcat
- (subseq s 0 where)
- new
- (string-substitute old new (subseq s (+ where (length old)))))
- s))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; string-left
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Like CAR for strings
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun string-left (s) (char s 0))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; string-rest
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Like CDR for strings
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun string-rest (s) (subseq s 1))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; to-string
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun to-string (x)
- ; char->string from string-primitive module
- (case (type-of x)
- (fixnum (string (int-char x)))
- (string x) ; now handles characters
- ))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; list-of-characters-to-string
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun list-of-characters-to-string (l)
- (if l
- (let ((stream (make-string-output-stream)))
- (dolist (c l) (write-char c stream))
- (get-output-stream-string stream))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; primitive-number-to-string
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun primitive-number-to-string (n)
- (let ((stream (make-string-output-stream)))
- (princ n stream)
- (get-output-stream-string stream)))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; *newline-string*
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; handy string consisting of one newline
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defconstant *newline-string* (string #\newline))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; zap-to-string
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun zap-to-string (uh)
- (cond
- ((listp uh) (list-to-string uh))
- ((symbolp uh)
- (let ((s (get uh 'as-a-string)))
- (or s
- (let ((s1 (string uh)))
- (putprop uh s1 'as-a-string)
- s1))))
- ((numberp uh) (number-to-string uh))
- (t (string uh))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; list-to-string
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun list-to-string (l)
- ; concatenate from the sequence module
- (if (null l)
- ""
- (if (equal (length l) 1)
- (zap-to-string (car l))
- (concatenate 'string
- (zap-to-string (car l))
- " "
- (list-to-string (cdr l))))))
-
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; number-to-string
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun number-to-string (n)
- ; round from math module
- ; trim-float from math module
- (case (type-of n)
- (float (if (> (abs n) 100000.0)
- (primitive-number-to-string (round n))
- (if (< (abs n) 1.0) (format nil "~F" (trim-float n 8))
- (format nil "~F" (trim-float n 2)))))
-
- ; No ratios in XLISP yet
- ; (ratio (if (> (abs n) 100)
- ; (number-to-string (coerce n 'float))
- ; (let* ((uh (multiple-value-list (truncate n)))
- ; (whole (first uh))
- ; (fraction (second uh)))
- ; (if (= fraction 0)
- ; (format nil "~D" whole)
- ; (format nil "~D-~D" whole fraction)))))
-
- (t (primitive-number-to-string n))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; numbered-list-string
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun numbered-list-string (l &key (indent 0) special)
- ; concatenate from the sequence module
- ; iota from apl module
- (flet
- ((formatter (x n)
- (concatenate 'string
- (make-string indent)
- "["
- (primitive-number-to-string n)
- "] "
- (if (and special (member x special :test #'equal))
- (string-upcase (zap-to-string x))
- (zap-to-string x))
- *newline-string*)))
- (apply #'concatenate
- (cons 'string
- (mapcar #'formatter l (mapcar #'1+ (iota (length l))))))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; list-string
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun list-string (l &key (indent 0) special)
- (flet
- ((formatter (x)
- (concatenate 'string
- (make-string indent)
- (if (and special (member x special :test #'equal))
- (string-upcase (zap-to-string x))
- (zap-to-string x))
- *newline-string*)))
- (apply #'concatenate (cons 'string (mapcar #'formatter l)))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; *big-long-string*
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defconstant *big-long-string*
- ; concatenate from the sequence module
- (let ((ten-spaces " ")
- (result ""))
- (dotimes (i 100) (setq result (concatenate 'string ten-spaces result)))
- result))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; make-string
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun make-string (big) (subseq *big-long-string* 0 big))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; read-from-string
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun read-from-string (string)
- (read (make-string-input-stream string)))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; remove-hyphens-and-downcase
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun remove-hyphens-and-downcase (str)
- ; substitute from sequence module
- (substitute #\space #\- (string-downcase str)))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; *vowels*
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defvar *vowels* '(#\a #\e #\i #\o #\u))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; begins-with-a-vowel-p
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun begins-with-a-vowel-p (string)
- (member (char string 0) *vowels*))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; word-plus-indefinite-article
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun word-plus-indefinite-article (str)
- (if (begins-with-a-vowel-p str)
- (format nil "an ~A" str)
- (format nil "a ~A" str)))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; right-justify
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun right-justify (string field-width)
- (let
- ((big (length string)))
- (if (< big field-width)
- (concatenate 'string (make-string (- field-width big)) string)
- string)))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; left-justify
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun left-justify (string field-width)
- (let
- ((big (length string)))
- (if (< big field-width)
- (concatenate 'string string (make-string (- field-width big)))
- string)))
-